# -*- coding: utf-8 -*-

"""常用的计算"""

from decimal import Decimal, ROUND_UP, ROUND_DOWN, ROUND_HALF_UP

def rounding(num, type):
    '''
    type:
    1：四舍五入到分
    2：四舍五入到角
    3：四舍五入到元
    4：见分进角
    5：见分舍去,见角进元
    6：向上舍入到分
    7：见角舍去
    8：见厘舍去
    9：四舍五入到厘
    '''
    if isinstance(num, (int, float, str)):
        num = Decimal(str(num))
    elif isinstance(num, Decimal):
        num = num

    if type == 1:
        return num.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
    if type == 2:
        return num.quantize(Decimal('0.1'), rounding=ROUND_HALF_UP)
    if type == 3:
        return num.quantize(Decimal('1'), rounding=ROUND_HALF_UP)
    if type == 4:
        num = num.quantize(Decimal('0.01'), rounding=ROUND_DOWN)
        return num.quantize(Decimal('0.1'), rounding=ROUND_UP)
    if type == 5:
        num = num.quantize(Decimal('0.1'), rounding=ROUND_DOWN)
        return num.quantize(Decimal('1'), rounding=ROUND_UP)
    if type == 6:
        return num.quantize(Decimal('0.01'), rounding=ROUND_UP)
    if type == 7:
        return num.quantize(Decimal('1'), rounding=ROUND_DOWN)
    if type == 8:
        return num.quantize(Decimal('0.01'), rounding=ROUND_DOWN)
    if type == 9:
        return num.quantize(Decimal('0.001'), rounding=ROUND_HALF_UP)